<template>
  <div class="router-tutorial">
    <div class="tutorial-content">
      <h2>命名路由</h2>
      <p>除了使用路径来标识路由外，我们还可以给路由命名，这在一些场景下会更加方便。</p>
      
      <h3>定义命名路由</h3>
      <p>在路由配置中添加 <code>name</code> 属性即可定义命名路由：</p>
      <pre>
const routes = [
  {
    path: '/',
    name: 'home',
    component: Home
  },
  {
    path: '/about',
    name: 'about',
    component: About
  },
  {
    path: '/user/:id',
    name: 'user',
    component: User
  }
]</pre>
      
      <h3>链接到命名路由</h3>
      <p>在模板中使用 <code>&lt;router-link&gt;</code> 时，可以通过 <code>:to</code> 属性传入一个对象来链接到命名路由：</p>
      <pre>
&lt;router-link :to="{ name: 'user', params: { id: 123 } }"&gt;用户页面&lt;/router-link&gt;</pre>
      
      <h3>编程式导航到命名路由</h3>
      <p>同样，在 JavaScript 代码中也可以使用命名路由：</p>
      <pre>
// 导航到命名路由
router.push({ name: 'user', params: { id: 123 } })</pre>
      
      <h3>命名路由的优势</h3>
      <p>使用命名路由的优势：</p>
      <ul>
        <li>无需硬编码URL路径</li>
        <li>URL路径变化时，无需修改链接</li>
        <li>更清晰的代码结构</li>
        <li>自动处理编码</li>
      </ul>
    </div>
    
    <div class="code-editor">
      <div class="file-tabs">
        <button @click="currentFile = 'router'">router.js</button>
        <button @click="currentFile = 'App'">App.vue</button>
        <button @click="currentFile = 'Component'">Component.vue</button>
      </div>
      
      <div class="code-preview" v-if="currentFile === 'router'">
        <pre><code>{{ routerCode }}</code></pre>
      </div>
      
      <div class="code-preview" v-if="currentFile === 'App'">
        <pre><code>{{ appCode }}</code></pre>
      </div>
      
      <div class="code-preview" v-if="currentFile === 'Component'">
        <pre><code>{{ componentCode }}</code></pre>
      </div>
      
      <div class="result-preview">
        <h4>预览效果：</h4>
        <div class="preview-container">
          <div class="app-container">
            <h2>命名路由示例</h2>
            <div class="nav-links">
              <a href="#" @click.prevent="changeRoute('home')">首页</a> | 
              <a href="#" @click.prevent="changeRoute('about')">关于</a> | 
              <a href="#" @click.prevent="changeRoute('user', { id: 123 })">用户 ID:123</a> | 
              <a href="#" @click.prevent="changeRoute('user', { id: 456 })">用户 ID:456</a>
            </div>
            <div class="route-info">
              <p>当前路由名称: <strong>{{ currentRoute }}</strong></p>
              <p v-if="params.id">路由参数: <strong>id = {{ params.id }}</strong></p>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';

const currentFile = ref('router');
const currentRoute = ref('home');
const params = ref({});

function getRouterCode() {
  return `// router.js - 使用命名路由
import { createRouter, createWebHistory } from 'vue-router'
import Home from './components/Home.vue'
import About from './components/About.vue'
import User from './components/User.vue'
import NotFound from './components/NotFound.vue'

const routes = [
  {
    path: '/',
    name: 'home',     // 首页的命名路由
    component: Home
  },
  {
    path: '/about',
    name: 'about',    // 关于页面的命名路由
    component: About
  },
  {
    path: '/user/:id',
    name: 'user',     // 用户页面的命名路由
    component: User
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'not-found',  // 404页面的命名路由
    component: NotFound
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

export default router`;
}

function getAppCode() {
  return `<template>
  <div id="app">
    <h2>命名路由示例</h2>
    <div class="nav-links">
      <!-- 使用命名路由的方式来链接 -->
      <router-link :to="{ name: 'home' }">首页</router-link> | 
      <router-link :to="{ name: 'about' }">关于</router-link> | 
      <router-link :to="{ name: 'user', params: { id: 123 } }">
        用户 ID:123
      </router-link> | 
      <router-link :to="{ name: 'user', params: { id: 456 } }">
        用户 ID:456
      </router-link>
    </div>
    <router-view></router-view>
  </div>
</template>

<script setup>
// App.vue 不需要特殊逻辑
<\/script>

<style>
.nav-links {
  margin: 20px 0;
}

.nav-links a {
  margin: 0 5px;
  color: #2c3e50;
  text-decoration: none;
}

.nav-links a.router-link-active {
  color: #42b883;
  font-weight: bold;
}
</style>`;
}

function getComponentCode() {
  return `<template>
  <div class="component">
    <h3>组件中的路由导航</h3>
    <p>在组件内部，我们可以使用编程式导航到命名路由</p>
    
    <div class="actions">
      <button @click="goToHome">返回首页</button>
      <button @click="goToUser">随机用户</button>
    </div>
  </div>
</template>

<script setup>
import { useRouter } from 'vue-router';

const router = useRouter();

function goToHome() {
  // 使用命名路由导航到首页
  router.push({ name: 'home' });
}

function goToUser() {
  // 生成随机用户ID并导航
  const randomId = Math.floor(Math.random() * 1000);
  router.push({ 
    name: 'user',
    params: { id: randomId }
  });
}
<\/script>

<style scoped>
.component {
  padding: 20px;
  background-color: #f8f8f8;
  border-radius: 8px;
}

.actions {
  margin-top: 15px;
}

button {
  margin-right: 10px;
  padding: 5px 10px;
  background-color: #42b883;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}
</style>`;
}

// 使用函数返回字符串的方式，避免嵌套模板字符串的问题
const routerCode = getRouterCode();
const appCode = getAppCode();
const componentCode = getComponentCode();

function changeRoute(route, routeParams = {}) {
  currentRoute.value = route;
  params.value = routeParams;
}
</script>

<style scoped>
.router-tutorial {
  display: flex;
  height: 100%;
}

.tutorial-content {
  flex: 1;
  padding: 20px;
  overflow-y: auto;
  border-right: 1px solid #ddd;
}

.code-editor {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.file-tabs {
  display: flex;
  background-color: #f0f0f0;
  border-bottom: 1px solid #ddd;
}

.file-tabs button {
  padding: 8px 16px;
  border: none;
  background: none;
  cursor: pointer;
}

.file-tabs button:hover {
  background-color: #e0e0e0;
}

.code-preview {
  flex: 1;
  overflow-y: auto;
  padding: 10px;
  background-color: #f9f9f9;
}

pre {
  margin: 0;
  white-space: pre-wrap;
  font-family: monospace;
}

.result-preview {
  height: 300px;
  border-top: 1px solid #ddd;
  padding: 10px;
  overflow-y: auto;
}

.preview-container {
  border: 1px solid #ddd;
  border-radius: 5px;
  padding: 15px;
  background-color: white;
}

.app-container {
  text-align: center;
}

.nav-links {
  margin: 20px 0;
}

.nav-links a {
  margin: 0 5px;
  color: #2c3e50;
  text-decoration: none;
}

.nav-links a:hover {
  color: #42b883;
}

.route-info {
  margin-top: 20px;
  padding: 15px;
  background-color: #f5f5f5;
  border-radius: 5px;
  text-align: left;
}
</style> 